<?php

namespace App\Models\Users;

use App\Models\BasicModel;
use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class UserSocialite extends BasicModel
{
    use HasDateTimeFormatter;
    use SoftDeletes;

    protected $table = 'user_socialite';


    const WECHAT = 1;
    const APPLE = 2;

    /**
     * @param $openid
     * @param $type
     * @return UserSocialite|\Illuminate\Database\Eloquent\Builder|Model|object|null
     */
    public function getSocialite($openid, $type, $request = [])
    {
        $where['open_id'] = $openid;
        $where['type'] = $type;
        $data['avatar'] = $request['avatar'] ?? '';
        $data['nickname'] = $request['nickname'] ?? '用户' . rand(10000, 99999);
        return self::updateOrCreate($where, $data);
    }


    /**
     * @param $id
     * @param $uid
     * @return bool|int
     */
    public function updateUid($id, $uid)
    {
        $update['uid'] = $uid;
        return self::where('id', $id)->update($update);
    }



}
